package com.wwg.entity;

import com.google.common.base.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.Iterator;


public class PagingList<P> implements Iterable<P> {
	private static final Logger LOGGER = LoggerFactory.getLogger(PagingList.class);
	/**
	 * 分页查询每页查询记录条数
	 */
	private int pageSize = 65500;

	/**
	 * 当前页
	 */
	private int currentPage;
	
	/**
	 * 最后一页
	 */
	private int lastPage;

	/**
	 * 长度
	 */
	private int length;

	/**
	 * 查询条件
	 */
	private P condition;

	public PagingList(int pageSize, P condition) {
		this.pageSize = pageSize;
		/*if (condition instanceof PagerModel) {
			PagerModel condi = (PagerModel) condition;
			this.currentPage = (condi.getRows()/pageSize)*(condi.getPage()-1)+1;
			this.length = condi.getRows();
			this.lastPage = this.currentPage + (condi.getRows()/pageSize) -1;
		} else */if (condition instanceof PagingVo) {
			PagingVo condi = (PagingVo) condition;
			this.currentPage = (condi.getRows()/condi.getPage())*(condi.getPage()-1)+1;
			this.length = condi.getRows();
			this.lastPage = this.currentPage + (condi.getRows()/pageSize) -1;
		} /*else if (condition instanceof com.wsn.jfpt.common.model.PagerModel){
			com.wsn.jfpt.common.model.PagerModel condi = (com.wsn.jfpt.common.model.PagerModel) condition;
			this.currentPage = (condi.getRows()/pageSize)*(condi.getPage()-1)+1;
			this.length = condi.getRows();
			this.lastPage = this.currentPage + (condi.getRows()/pageSize) -1;
		} */else {
			throw new IllegalArgumentException("condition must be a subtype of PagerModel or PagingVo");
		}
		this.condition = condition;
	}

	private class Itr implements Iterator<P> {
		Itr() {
		}

		@Override
		public boolean hasNext() {
			if (currentPage <= lastPage) {
				return true;
			}
			return false;
		}

		@SuppressWarnings("unchecked")
		@Override
		public P next() {
			try {
				final P pagingCondition = (P) condition.getClass().newInstance();
				/*if (pagingCondition instanceof PagerModel) {
					PagerModel condi = (PagerModel) pagingCondition;
					BeanUtils.copyProperties(condition, condi);
					condi.setLimitStart(0);
					condi.setLength(0);
					condi.setPage(currentPage++);
					condi.setRows(pageSize);
				} else*/ if (pagingCondition instanceof PagingVo) {
					PagingVo condi = (PagingVo) pagingCondition;
					BeanUtils.copyProperties(condition, condi);
					condi.setPage(currentPage++);
					condi.setRows(pageSize);
				} /*else if (condition instanceof com.wsn.jfpt.common.model.PagerModel){
					com.wsn.jfpt.common.model.PagerModel condi = (com.wsn.jfpt.common.model.PagerModel) pagingCondition;
					BeanUtils.copyProperties(condition, condi);
					condi.setLimitStart(0);
					condi.setLength(0);
					condi.setPage(currentPage++);
					condi.setRows(pageSize);
				}*/ else {
					throw new IllegalArgumentException("condition must be a subtype of PagerModel or PagingVo");
				}

				return pagingCondition;
			} catch (InstantiationException | IllegalAccessException e) {
				LOGGER.error(e.getMessage());
			} 
            return null;
		}
	}

	@Override
	public Iterator<P> iterator() {
		return new Itr();
	}

	public int size() {
		Preconditions.checkArgument(this.length >= 0, "count must be greater than or equal to 0");
		Preconditions.checkArgument(this.pageSize > 0, "pageSize must be greater than 0");
		return (int) (this.length == 0 ? 0 : (this.length - 1) / this.pageSize + 1);
	}
	
}
